package org.tabcap.workspace.document;

import java.awt.Font;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultCaret;
import javax.swing.undo.UndoManager;

import com.meg.utils.file.SimpleDataUtils;

public class TabCapDocument implements BaseDocument, DocumentListener
{
	protected JTextPane textPane;
	public JTextPane textPane() { return textPane; }
	@Override public void useTextPane(JTextPane textPane)
	{
		this.textPane = textPane;
		textPane.getDocument().addUndoableEditListener(undo = new CompoundUndoManager(textPane));
		textPane.getDocument().addDocumentListener(this);
		DefaultCaret caret = (DefaultCaret)textPane.getCaret();
		caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
		
		if(file.exists() && !file.isDirectory() && file.canRead())
			textPane.setText(SimpleDataUtils.fileToText(file));
	}
	public String text() { return textPane.getText(); }
	
	protected JLabel label;
	public JLabel label() { return label; }
	@Override public void useLabel(JLabel label)
	{
		this.label = label;
		label.setText(name());
	}
	
	protected boolean initialized;
	public boolean initialized() { return initialized; }
	public void setInitialized(boolean initialized) { this.initialized = initialized; }
	
	protected File file;
	protected boolean validFile;
	protected boolean isWorthless;
	protected boolean needsSave;
	protected UndoManager undo;
	
	public TabCapDocument(int number)
	{
		file = new File("Untitled " + number);
		validFile = false;
		isWorthless = true;
		needsSave = true;
	}
	
	public TabCapDocument(String filename) throws Exception
	{
		this(new File(filename));
	}
	
	public TabCapDocument(File file) throws Exception
	{
		this.file = file;
		if(!file.exists() || file.isDirectory())
			throw new Exception("Not a valid file to open!");
		validFile = true;
		isWorthless = false;
		needsSave = false;
	}

	public void changedUpdate(DocumentEvent e) { }
	public void insertUpdate(DocumentEvent e) { documentChanged(); }
	public void removeUpdate(DocumentEvent e) { documentChanged(); }
	
	public void documentChanged()
	{
		if(initialized)
		{
			needsSave = true;
			isWorthless = false;
			label.setText("*" + name());
			label.setFont(label.getFont().deriveFont(Font.BOLD));
		}
	}

	public String name() { return file.getName(); }
	public String directory() { return file.exists() ? file.getParentFile().getAbsolutePath() : null; }
	public File file() { return file; }

	public boolean validFile() { return validFile; }
	public boolean isWorthless() { return isWorthless; }
	public boolean needsSave() { return needsSave; }

	public void save()
	{
		if(validFile)
		{
			SimpleDataUtils.textToFile(textPane.getText(), file);
			needsSave = false;
			label.setText(name());
			label.setFont(label.getFont().deriveFont(Font.PLAIN));
		}
	}

	public void saveAs(File file)
	{
		this.file = file;
		validFile = true;
		save();
	}

	public void undo() { if(undo.canUndo()) undo.undo(); }
	public void redo() { if(undo.canRedo()) undo.redo(); }
	public void cut() { textPane.cut(); }
	public void copy() { textPane.copy(); }
	public void paste() { textPane.paste(); }

	@Override public void compile() { }
	@Override public void run() { }
}